<template>
  <div class="add_all">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>管理员管理</el-breadcrumb-item>
      <el-breadcrumb-item>添加管理员</el-breadcrumb-item>
    </el-breadcrumb>
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="账户名" prop="adminsName">
        <el-input v-model.number="ruleForm.adminsName"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="adminsPass">
        <el-input type="password" v-model="ruleForm.adminsPass" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkAdminsPass">
        <el-input type="password" v-model="ruleForm.checkAdminsPass" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { createNamespacedHelpers } from "vuex";
const { mapState, mapActions, mapMutations } = createNamespacedHelpers(
  "admins"
);
export default {
  data() {
    //验证
    var checkName = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("用户名不能为空"));
      }
      callback();
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkAdminsPass !== "") {
          this.$refs.ruleForm.validateField("checkAdminsPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.adminsPass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      rules: {
        adminsPass: [{ validator: validatePass, trigger: "blur" }],
        checkAdminsPass: [{ validator: validatePass2, trigger: "blur" }],
        adminsName: [{ validator: checkName, trigger: "blur" }],
      },
    };
  },
  computed: {
    ...mapState(["ruleForm"]),
  },
  methods: {
    ...mapActions(["addAdmins"]),
    ...mapMutations(["reset"]),
    async submitForm(formName) {
      const data = await this.addAdmins();
      if (data.status) {
        alert("管理员" + data.data.adminName + "添加成功！");
      }
    },
    resetForm(formName) {
      this.reset();
    },
  },
};
</script>

<style lang='scss' scoped>
@import "./css/addAdmins.scss";
</style>